const binance = require('../../common/btb/binance')

exports.getExchangeInfo = async function (ctx) {
  try {
    const data = await binance.exchangeInfo()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.getAccount = async function (ctx) {
  try {
    const data = await binance.account()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.getBalance = async function (ctx) {
  try {
    const data = await binance.balance()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.getPrices = async function (ctx) {
  try {
    const data = await binance.prices()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.getPricesByKey = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.pricesByKey(query.key)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.marketBuy = async function (ctx) {
  const query = ctx.query
  try {
    // {
    //   "price": "4000.00000000", // 交易的价格
    //   "qty": "1.00000000", // 交易的数量
    //   "commission": "4.00000000", // 手续费金额
    //   "commissionAsset": "USDT" // 手续费的币种
    // },
    // executedQty: 交易的订单数量
    // cummulativeQuoteQty: 交易时消耗的本币
    const data = await binance.marketBuy(query.key, query.quantity)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.marketSell = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.marketSell(query.key, query.quantity)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.kLines = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.kLines(query.key, query.interval)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.kLinesAll = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.kLinesAll(query.key, query.interval, query.count)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresKLinesAll = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresKLinesAll(query.key, query.interval, query.count)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.kLinesAllWithTime = async function (ctx) {
  const query = ctx.query
  try {
    const start = (new Date()).getTime()
    const data = await binance.kLinesAll(query.key, query.interval)
    ctx.body = ctx.resuccess({
      data,
      time: (new Date()).getTime() - start
    })
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.kLinesAllOnlyTime = async function (ctx) {
  const query = ctx.query
  try {
    const start = (new Date()).getTime()
    await binance.kLinesAll(query.key, query.interval)
    ctx.body = ctx.resuccess({
      time: (new Date()).getTime() - start
    })
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}
// 获取交易规则
exports.getFuturesExchangeInfo = async function (ctx) {
  try {
    const data = await binance.futuresExchangeInfo()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}
// 获取合约资产
exports.getFuturesAccount = async function (ctx) {
  try {
    const data = await binance.futuresAccount()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}
// 获取价格
exports.getFuturesPrices = async function (ctx) {
  try {
    const data = await binance.futuresPrices()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}
// 获取24小时交易情况
exports.getFuturesDaily = async function (ctx) {
  try {
    const data = await binance.futuresDaily()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.getFuturesBalance = async function (ctx) {
  try {
    const data = await binance.futuresBalance()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.getFuturesPositionRisk = async function (ctx) {
  try {
    const data = await binance.futuresPositionRisk()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresLeverage = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresLeverage(query.key, query.quantity)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresMarketBuy = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresMarketBuy(query.key, query.quantity)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresMarketSell = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresMarketSell(query.key, query.quantity)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresBuy = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresBuy(query.key, query.quantity, query.price)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresSell = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresSell(query.key, query.quantity, query.price)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresCancel = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresCancel(query.key, query.orderId)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresCancelAll = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresCancelAll(query.key)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresAllOrders = async function (ctx) {
  try {
    const data = await binance.futuresAllOrders()
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.orderStatus = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.orderStatus(query.key, query.orderId)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresOrderStatus = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresOrderStatus(query.key, query.orderId)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresMarginType = async function (ctx) {
  const query = ctx.query
  try {
    // 逐ISOLATED, 全CROSSED
    const data = await binance.futuresMarginType(query.symbol, query.marginType)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresQuote = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresQuote(query.symbol)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}

exports.futuresDepth = async function (ctx) {
  const query = ctx.query
  try {
    const data = await binance.futuresDepth(query.symbol)
    ctx.body = ctx.resuccess(data)
  } catch (err) {
    console.log(err)
    ctx.body = ctx.refail(err)
  }
}
